<?php
class WriteController extends  Controller{
	
	public function index(){  
		 $this->getSmarty();
        require_once COMMON . DS  . 'Constant.class.php';
        $this->smarty->assign ( 'hottags',Constant::getHotTag() );
        $this->smarty->assign ( 'hotcate',Constant::getHotCate() );

		 $this->smarty->display("admin/posts/write.tpl"); 
	}
	public function update(){
		$id = $_GET['id'];
		$this->getSmarty();
        require_once SERVICE . DS . 'admin/CommonService.class.php';
        $commonService = new CommonService( $this->getDB () );
        $postsql = "select * from posts where id=$id";
        $posts = $commonService->query($postsql);
        if(!empty($posts))
          $this->smarty->assign("post",$posts[0]);
        //get tags
        $tagssql = "select tag.tag_id id,tag.tag_name name from tags tag,post_tag pt where tag.tag_id = pt.tag_id and pt.post_id = $id";

        $tags = $commonService->query($tagssql);
        if(!empty($tags))
            $this->smarty->assign("tags",$tags);

        require_once COMMON . DS  . 'Constant.class.php';
        $this->smarty->assign ( 'hottags',Constant::getHotTag() );
        $this->smarty->assign ( 'hotcate',Constant::getHotCate() );
		 $this->smarty->display("admin/posts/write.tpl"); 
	}
	
	public function saveOrUpdate(){
		 $id = $title = $markdowncontent = $htmlcontent = $category = $tags = $post_category = $post_status = $post_file_location=$post_excerpt='';
	     extract ( $_POST, EXTR_IF_EXISTS ); 
		 require_once SERVICE . DS . 'admin/CommonService.class.php';
		 $commonService = new CommonService( $this->getDB () );
         if($htmlcontent != ''){
             require_once COMMON . DS  . 'Constant.class.php';
             $post_excerpt= Constant::cutArticle($htmlcontent,300);
         }
		 $datas = array("post_author"=>$_SESSION['aduser']->id,"post_content"=>stripslashes($htmlcontent),"post_title"=>$title,"post_markdowncontent"=>$markdowncontent,"post_status"=>$post_status,
		                "post_category"=>$post_category,"id"=>$id,"post_file_location"=>$post_file_location,"post_excerpt"=>$post_excerpt);
        $tag_str='';
         if($tags != ''){
             foreach($tags as $tag){
                 $tag_str.=$tag['name'].",";
             }
         }
        $datas["tags"] = substr($tag_str,0,strlen($tag_str)-1);
		 if($id == ''){ 
		 	$datas["post_date"] = date("Y-m-d H:i",time());
		 	unset($datas['id']);
		 	$id = $commonService->insert("posts",$datas);
		 	$datas["id"] =  $id;
		 
		 } else{
		 	  $where = array("ID"=>$id);
		 	  $commonService->update("posts",$datas,$where);
		 } 
		 //upate tag
         $deltags = "delete from post_tag where post_id=".$datas['id'];
         $commonService->del($deltags);
            if($tags != ''){
                foreach($tags as $tag){
                    $tagdata = array("post_id"=>$datas["id"],"tag_id"=>$tag['id'],"create_date"=>date("Y-m-d H:i",time()));
                    $commonService->insert("post_tag",$tagdata);
                }
            }
        unset($datas['post_excerpt']);
		echo json_encode($datas);
	}
 public function imgupload(){
 	   
	    $sysname = $_POST["sysname"]; 
	    
        $name =date("Y-m-d_H-i-s");
        $direct =date("Y-m-d");
        $uploaddir = PROJECT.DS."upload".DS;//上传路径
        $pinfo=pathinfo($_FILES['msUploadFile']['name']); 
        $ftype=$pinfo[extension];
        
         if(!file_exists($uploaddir.$direct)){
         	//echo $uploaddir.$direct;
            mkdir($uploaddir.$direct);
         }
         $filepath = $direct.DS .$name.".".$ftype;
         $datas = array("file"=>$filepath);
         
        $destination = $uploaddir.$filepath; 
		
		 if(move_uploaded_file($_FILES['msUploadFile']['tmp_name'],$destination)){
 				require_once SERVICE . DS . 'admin/CommonService.class.php';
 		        $common = new CommonService($this->getDB());
		   //     $common->insert("tca_check_list",array("BEGINTIME"=>$direct,"SYSTEM_NAME"=>$sysname,"OPERATE_NAME"=>$peruser,"FILE_NAME"=>$filepath));
		     //   $this->analyzePolling($destination,$filepath);
		       require_once UTIL . DS . 'ImgThumb.php';
 		       $imgthumbname = $name."_thumb.".$ftype; 
 		       $imgthumbfull = $uploaddir.$direct.DS.$imgthumbname;
 		       $isthumb = img2thumb($destination, $imgthumbfull, $width = 200, $height = 300, $cut = 0, $proportion = 0);
		     $datas['showsrc'] = WEBSITE_URL."upload/".$direct."/".$name.".".$ftype;
		     $datas['showsrcthumb'] = WEBSITE_URL."upload/".$direct."/".$imgthumbname;
		   echo json_encode($datas);	
		 }else{
			echo 'fail';	
		 } 
 }  
}